package com.yiqi.liebang.common.rongim.ui;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;

import com.alibaba.fastjson.JSONObject;
import com.suozhang.framework.utils.T;
import com.suozhang.framework.utils.logger.Logger;
import com.yiqi.liebang.R;
import com.yiqi.liebang.common.rongim.msg.CustomizeMessage;
import com.yiqi.liebang.feature.people.view.FriendsListActivity;

import cn.rongcloud.contactcard.Friend;
import io.rong.imkit.RongExtension;
import io.rong.imkit.RongIM;
import io.rong.imkit.plugin.IPluginModule;
import io.rong.imkit.plugin.IPluginRequestPermissionResultCallback;
import io.rong.imlib.IRongCallback;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;

public class ContactPlugin implements IPluginModule, IPluginRequestPermissionResultCallback {

    Conversation.ConversationType conversationType;
    String targetId;
    Context mContext;

    @Override
    public Drawable obtainDrawable(Context context) {
        return context.getResources().getDrawable(R.drawable.rc_contact_plugin_icon_unpressed);
    }

    @Override
    public String obtainTitle(Context context) {
        mContext = context;
        return "个人名片";

    }

    @Override
    public void onClick(Fragment currentFragment, RongExtension extension) {
        this.conversationType = extension.getConversationType();
        this.targetId = extension.getTargetId();
        Intent intent = new Intent(currentFragment.getActivity(), FriendsListActivity.class);
        intent.putExtra("is_select", true);
        extension.startActivityForPluginResult(intent, 23, this);

    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 23) {
            try {

                Friend friend = data.getParcelableExtra("three");
                if (friend == null) return;
                String json = JSONObject.toJSONString(friend);
                Logger.e("Friend------>"+json);
                CustomizeMessage message = CustomizeMessage.obtain(
                        "",
                        "",
                        targetId,
                        "",
                        json,
                        "",
                        friend.getIsEducation(),
                        friend.getUserUid(),
                        friend.getUserName(),
                        friend.getIsBasic(),
                        friend.getUserNamePY(),
                        friend.getIsOccupation(),
                        friend.getUserHead(),
                        friend.getPosition(),
                        friend.getMechanism(),
                        friend.getComLogo(),
                        friend.getCompany(),
                        friend.getUserClassify(),
                        friend.getUserEmail(),
                        friend.getUserWorkAddress(),
                        friend.getEffectSocre(),
                        friend.getUserPhone(),
                        friend.getPhonePrivacy(),
                        friend.getPhone()
                );


                RongIM.getInstance().sendMessage(Message.obtain(targetId, conversationType, message), "sass", "sss",
                        new IRongCallback.ISendMessageCallback() {
                            @Override
                            public void onAttached(Message message) {

                            }

                            @Override
                            public void onSuccess(Message message) {

                            }

                            @Override
                            public void onError(Message message, RongIMClient.ErrorCode errorCode) {
                                T.showShort("onError" + errorCode.getMessage());
                            }
                        });
            } catch (Exception e) {
                Logger.e("Exception--->" + e.getMessage());
            }


        }
    }

    @Override
    public boolean onRequestPermissionResult(Fragment fragment, RongExtension rongExtension, int i, @NonNull String[] strings, @NonNull int[] ints) {
        return false;
    }

}